
package Interface;

import java.sql.SQLException;
import javax.swing.JOptionPane;
import Utilitarios.conexao;

public class BemVindo extends javax.swing.JFrame {


    public BemVindo() {
        initComponents();
    }

  
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        JL_nome_login = new javax.swing.JLabel();
        JL_senha = new javax.swing.JLabel();
        JPs_senha_login = new javax.swing.JPasswordField();
        JB_entrar = new javax.swing.JButton();
        JB_sair = new javax.swing.JButton();
        JT_nome_login = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel1.setIcon(new javax.swing.ImageIcon("F:\\UnB_logo.jpg")); // NOI18N

        JL_nome_login.setText("Nome.:");

        JL_senha.setText("Senha.:");

        JPs_senha_login.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                JPs_senha_loginActionPerformed(evt);
            }
        });

        JB_entrar.setText("Entrar");
        JB_entrar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                JB_entrarActionPerformed(evt);
            }
        });

        JB_sair.setText("Sair");
        JB_sair.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                JB_sairActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(33, 33, 33)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(JB_entrar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(JB_sair))
                    .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 416, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(JL_senha)
                            .addComponent(JL_nome_login))
                        .addGap(5, 5, 5)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(JT_nome_login, javax.swing.GroupLayout.DEFAULT_SIZE, 366, Short.MAX_VALUE)
                            .addComponent(JPs_senha_login, javax.swing.GroupLayout.DEFAULT_SIZE, 366, Short.MAX_VALUE))))
                .addGap(31, 31, 31))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(39, 39, 39)
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 151, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(JL_nome_login)
                    .addComponent(JT_nome_login, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(JL_senha)
                    .addComponent(JPs_senha_login, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(JB_entrar)
                    .addComponent(JB_sair))
                .addContainerGap(39, Short.MAX_VALUE))
        );

        setBounds(400, 200, 488, 370);
    }// </editor-fold>                        

    private void JB_entrarActionPerformed(java.awt.event.ActionEvent evt) {                                          

 String matUser=null;
        int typeUser=0;
        conexao banco=new conexao();

        banco.conecta();


        if(JT_nome_login.getText().equals("")==true|| JPs_senha_login.equals("")==true)
            JOptionPane.showMessageDialog(null,"por favor, preencha todos os campos!","Aviso",JOptionPane.WARNING_MESSAGE);
        else{
             if( banco.conecta()==true){
                 // fecha login anterior

                 try{
                         banco.executeSQL("select * from \"User_Log\" order by \"Id_Log\"");
                         banco.resultSet.last();

                        
                         banco.statement.executeUpdate("UPDATE \"User_Log\" SET \"Status\"='false' WHERE \"Id_User\"='"+banco.resultSet.getString("Id_User")+"'");



                     }catch(Exception error)
                     {
                         JOptionPane.showMessageDialog(null,error);
                     }


            try {

                //registra novo login
                if (banco.executeSQL("select * from \"User_1\" where \"Name_user\" ='" + JT_nome_login.getText() + "' and senha ='" + JPs_senha_login.getText() + "'") == true) {

                matUser=banco.resultSet.getString("Mat_user");
                typeUser=banco.resultSet.getInt("Level_access");

                JOptionPane.showMessageDialog(null, "Bem-vindo "+JT_nome_login.getText()+"!","",JOptionPane.PLAIN_MESSAGE);

                banco.statement.executeUpdate("insert into \"User_Log\" (\"Id_User\",\"User_Name\",\"User_Type\",\"Status\")" +
                        "values('"+matUser+"','"+JT_nome_login.getText()+"',"+typeUser+",true)");


                    new Principal().show();

                    this.dispose();



                } else if (JT_nome_login.getText().length()==0| JPs_senha_login.getText().length()==0){

                    JOptionPane.showMessageDialog(null, "Digite o seu nome e senha!","Aviso!",JOptionPane.WARNING_MESSAGE);

                }else{
                    
                     JOptionPane.showMessageDialog(null, "Nome do usuário ou senha incorreto!","Aviso",JOptionPane.WARNING_MESSAGE);
                     JT_nome_login.setText("");
                     JPs_senha_login.setText("");
                }

            } catch (Exception error) {

                JOptionPane.showMessageDialog(null, error);

            }
              // Logger.getLogger(login.class.getName()).log(Level.SEVERE, null, ex);

             }


        }


    }                                         

    private void JB_sairActionPerformed(java.awt.event.ActionEvent evt) {                                        


        this.disable();
    }                                       

    private void JPs_senha_loginActionPerformed(java.awt.event.ActionEvent evt) {                                                

        String matUser=null;
        int typeUser=0;
        conexao banco=new conexao();

        banco.conecta();


        if(JT_nome_login.getText().equals("")==true|| JPs_senha_login.equals("")==true)
            JOptionPane.showMessageDialog(null,"por favor, preencha todos os campos!","Aviso",JOptionPane.WARNING_MESSAGE);
        else{
             if( banco.conecta()==true){
                 // fecha login anterior

                 try{
                         banco.executeSQL("select * from \"User_Log\" order by \"Id_Log\"");
                         banco.resultSet.last();

                        
                         banco.statement.executeUpdate("UPDATE \"User_Log\" SET \"Status\"='false' WHERE \"Id_User\"='"+banco.resultSet.getString("Id_User")+"'");



                     }catch(Exception error)
                     {
                         JOptionPane.showMessageDialog(null,error);
                     }


            try {

                //registra novo login
                if (banco.executeSQL("select * from \"User_1\" where \"Name_user\" ='" + JT_nome_login.getText() + "' and senha ='" + JPs_senha_login.getText() + "'") == true) {

                matUser=banco.resultSet.getString("Mat_user");
                typeUser=banco.resultSet.getInt("Level_access");

                JOptionPane.showMessageDialog(null, "Bem-vindo "+JT_nome_login.getText()+"!","",JOptionPane.PLAIN_MESSAGE);

                banco.statement.executeUpdate("insert into \"User_Log\" (\"Id_User\",\"User_Name\",\"User_Type\",\"Status\")" +
                        "values('"+matUser+"','"+JT_nome_login.getText()+"',"+typeUser+",true)");

               
                    new Principal().show();

                    this.dispose();



                } else if (JT_nome_login.getText().length()==0| JPs_senha_login.getText().length()==0){

                    JOptionPane.showMessageDialog(null, "Digite o seu nome e senha!","Aviso!",JOptionPane.WARNING_MESSAGE);

                }else{
                    JOptionPane.showMessageDialog(null, "Nome do usuário ou senha incorreto!","Aviso",JOptionPane.WARNING_MESSAGE);
                     JT_nome_login.setText("");
                     JPs_senha_login.setText("");
                }

            } catch (Exception error) {

                JOptionPane.showMessageDialog(null, error);

            }
              // Logger.getLogger(login.class.getName()).log(Level.SEVERE, null, ex);

             }


        }
    }                                               

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new BemVindo().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify                     
    private javax.swing.JButton JB_entrar;
    private javax.swing.JButton JB_sair;
    private javax.swing.JLabel JL_nome_login;
    private javax.swing.JLabel JL_senha;
    private javax.swing.JPasswordField JPs_senha_login;
    private javax.swing.JTextField JT_nome_login;
    private javax.swing.JLabel jLabel1;
    // End of variables declaration                   

}
